﻿using AuthCenter.API.Application.Command.Report;
using AuthCenter.API.DTOs;
using AuthCenter.Domain.Request;
using AuthCenter.ErrorCode;
using AuthCenter.Intertructrue.Interface;
using MediatR;

namespace AuthCenter.API.Application.Handler.Report
{
    public class GetReportHandler : IRequestHandler<GetReportCommand, APIResult<List<ReportDTO>>>
    {
        private readonly IBaseRepository<RequestModel> requestRepo;

        public GetReportHandler(IBaseRepository<RequestModel> requestRepo)
        {
            this.requestRepo = requestRepo;
        }
        public Task<APIResult<List<ReportDTO>>> Handle(GetReportCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<ReportDTO>> result = new APIResult<List<ReportDTO>>();
            var res = requestRepo.GetAll().Where(x => !x.IsDel);
            var query = from a in res
                         where !a.IsDel && a.State == 2
                         group a by a.ApplicantName into g
                         select new ReportDTO
                         {
                             Name = g.Key,
                             One = g.Where(x => x.StartTime.Month == 1 && x.StartTime.Year == DateTime.Now.Year).Sum(x => x.TotalTime),
                             Two = g.Where(x => x.StartTime.Month == 2 && x.StartTime.Year == DateTime.Now.Year).Sum(x => x.TotalTime),
                             Three = g.Where(x => x.StartTime.Month == 3 && x.StartTime.Year == DateTime.Now.Year).Sum(x => x.TotalTime),
                             Four = g.Where(x => x.StartTime.Month == 4 && x.StartTime.Year == DateTime.Now.Year).Sum(x => x.TotalTime),
                             Five = g.Where(x => x.StartTime.Month == 5 && x.StartTime.Year == DateTime.Now.Year).Sum(x => x.TotalTime),
                             Six = g.Where(x => x.StartTime.Month == 6 && x.StartTime.Year == DateTime.Now.Year).Sum(x => x.TotalTime),
                             Seven = g.Where(x => x.StartTime.Month == 7 && x.StartTime.Year == DateTime.Now.Year).Sum(x => x.TotalTime),
                             Eight = g.Where(x => x.StartTime.Month == 8 && x.StartTime.Year == DateTime.Now.Year).Sum(x => x.TotalTime),
                             Nine = g.Where(x => x.StartTime.Month == 9 && x.StartTime.Year == DateTime.Now.Year).Sum(x => x.TotalTime),
                             Then = g.Where(x => x.StartTime.Month == 10 && x.StartTime.Year == DateTime.Now.Year).Sum(x => x.TotalTime),
                             Eleven = g.Where(x => x.StartTime.Month == 11 && x.StartTime.Year == DateTime.Now.Year).Sum(x => x.TotalTime),
                             Twelve = g.Where(x => x.StartTime.Month == 12 && x.StartTime.Year == DateTime.Now.Year).Sum(x => x.TotalTime),
                         };
            result.Message = "显示成功";
            result.Code = APIEnum.Ok;
            result.Data = query.ToList();
            return Task.FromResult(result);
        }
    }
}
